using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using IGlobalist.Cms.Model;
using Thycotic.Collections;

namespace IGlobalist.Cms.Bll
{
    public class VideoController : BaseController
    {
        private static Video ConvertCmsObject2Video(ICmsObject obj)
        {
            Video video = new Video();
            video.FromCmsObject(obj);
            video.Thumbnail = GetNameByAuthor(video.Author);
            if (obj == null) return null;
            video.NoteList = ContentNodeController.GetContentNoteListLelateToObjectID(obj.ID);
            return video;
        }

        private static IList<IVideo> ConvertListObject2ListVideo(IList<ICmsObject> list)
        {
            IList<IVideo> videos = new List<IVideo>();
            if (list != null)
                foreach (ICmsObject obj in list)
                {
                    Video v=ConvertCmsObject2Video(obj);
                    if (v.Sharing == true)
                    {
                        v.Thumbnail = GetNameByAuthor(v.Author);
                        videos.Add(v);
                    }
                }

            return videos;
        }

        public static VideoList ConvertListObject2VideoList(IList<ICmsObject> list)
        {
            VideoList videos = new VideoList();
            if (list != null)
                foreach (ICmsObject video in list)
                {
                    if (video.ClassTypeID == (short)CmsObjectType.Video)
                    {
                        Video v = ConvertCmsObject2Video(video);
                        if (v.Sharing == true)
                        {
                            v.Thumbnail = GetNameByAuthor(v.Author);
                            videos.Add(v);
                        }
                    }
                }
            return videos;
        }

        public static VideoList GetVideoListByCategoryId(long categoryId)
        {
            VideoList videoList = new VideoList();
            VideoList list = ConvertListObject2VideoList(_Ds.GetObjectListAllByClassTypeID((short)CmsObjectType.Video));

            foreach (Video video in list)
            {
                if ((video.CategoryID == categoryId) && (video.Sharing==true))
                {
                    video.Thumbnail = GetNameByAuthor(video.Author);
                    videoList.Add(video);
                }
            }
            return videoList;
        }

        public static VideoList GetVideoList()
        {
            VideoList videoList = new VideoList();
            VideoList list = ConvertListObject2VideoList(_Ds.GetObjectListAllByClassTypeID((short)CmsObjectType.Video));

            return list;
        }

        public static VideoList GetVideoViewList()
        {
            VideoList videoList = new VideoList();
            VideoList list = ConvertListObject2VideoList(_Ds.GetObjectListAllByClassTypeID((short)CmsObjectType.Video));

            PropertyComparer pc = new PropertyComparer();
            pc.AddPropertyName("Views", CompareOrder.Descending);
            ArrayList listCmt = new ArrayList();
            foreach (Video p in list)
                listCmt.Add(p);
            listCmt.Sort(pc);
            for (int i = 0; i < listCmt.Count; i++)
            {
                videoList.Add((IVideo)listCmt[i]);
            }

            return videoList;
        }

        public static VideoList GetVideoRateList()
        {
            VideoList videoList = new VideoList();
            VideoList list = ConvertListObject2VideoList(_Ds.GetObjectListAllByClassTypeID((short)CmsObjectType.Video));

            PropertyComparer pc = new PropertyComparer();
            pc.AddPropertyName("Rates", CompareOrder.Descending);
            ArrayList listCmt = new ArrayList();
            foreach (Video p in list)
                listCmt.Add(p);
            listCmt.Sort(pc);
            for (int i = 0; i < listCmt.Count; i++)
            {
                videoList.Add((IVideo)listCmt[i]);
            }

            return videoList;
        }

        public static VideoList GetVideoListByWhere(string where)
        {
            VideoList videoList = ConvertListObject2VideoList(_Ds.GetObjectListAllBySearch(where));
            return videoList;
        }

        public static VideoList GetVideoListBySearch(Hashtable where,string author)
        {
            VideoList videoListResult = new VideoList();
            VideoList videoList = ConvertListObject2VideoList(_Ds.GetObjectListAllByAdvancedSearch(where));
            if (author != "")
            {
                IList<IUser> ulist = UserController.GetUserListByWhere(author);
                if (ulist.Count > 0)
                    foreach (Video v in videoList)
                    {
                        v.Author = ulist[0].ID;
                        videoListResult.Add(v);
                    }
                return videoListResult;
            }
            else
                return videoList;
        }



        public static IList<IVideo> GetVideoList(long classID)
        {
            return ConvertListObject2ListVideo(_Ds.GetObjectListByClassID(classID));
        }

        public static Video GetVideo(long id)
        {
            return ConvertCmsObject2Video(_Ds.GetObject(id));
        }

        public static VideoList GetVideoListLelateToObjectID(long objectID, CmsObjectType objectType)
        {
            return ConvertListObject2VideoList(_Ds.GetObjectListRelateToObjectID(objectID, (short)objectType));
        }

        public static long InsertVideo(IVideo video)
        {
            long id = _Ds.InsertObject((ICmsObject)video);

            if (video.CategoryID > 0)
            {
                _Ds.InsertObjectRelation(video.CategoryID, id, (short)CmsObjectType.Video);
            }

            //InsertUpdateDeleleContentNode(id, video.NoteList);

            return id;
        }

        public static void UpdateVideo(Video video)
        {
             Video videoOld = GetVideo(video.ID);

            _Ds.UpdateObject((ICmsObject)video);

            if (videoOld.CategoryID != video.CategoryID)
            {
                _Ds.DeleteObjectOneRelation(videoOld.CategoryID, video.ID);
                if (video.CategoryID > 0)
                {
                    _Ds.InsertObjectRelation(video.CategoryID, video.ID, (short)CmsObjectType.Video);
                }
            }

            InsertUpdateDeleleContentNode(video.ID, video.NoteList);


            _Ds.ClearCacheData((short)CmsObjectType.Page);

        }

        public static void DeleteVideo(long id)
        {
            Video videoOld = GetVideo(id); 
            _Ds.DeleteObjectOneRelation(id, videoOld.CategoryID);
            _Ds.DeleteObject(id);
          
        }

        private static void InsertUpdateDeleleContentNode(long videoID, ContentNoteList nodeList)
        {
            if (nodeList != null && nodeList.Count > 0)
            {
                foreach (IContentNote contentNode in nodeList)
                {
                    switch (contentNode.ActionType)
                    {
                        case ActionType.Add:
                            //contentNode.ParentID = videoID;
                            long noteID = ContentNodeController.InsertContentNote(contentNode);
                           _Ds.InsertObjectRelation(videoID, noteID, (short)CmsObjectType.ContentNote);
                            break;
                        case ActionType.Edit:
                            ContentNodeController.UpdateContentNote(contentNode);
                            break;
                        case ActionType.Delete:
                            ContentNodeController.DeleteContentNote(contentNode.ID);
                            break;
                    }
                }
            }
        }

        private static string GetNameByAuthor(long author)
        {
            IUser u = new User();
            u = UserController.GetUser(author);
            return u.Name;
        }

    }
}
